package com.wontube.smr.bo.sites.fs;

import java.io.InputStream;
import java.util.UUID;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.wontube.smr.bo.sites.fs.api.Mp3FileInfoAPI;
import com.wontube.smr.bo.sites.fs.api.SimpleAPI;
import com.wontube.smr.client.model.vo.AudioFile;
import com.wontube.smr.client.model.vo.AudioFiles;



/**
 * @author yuechao
 * 
 */
final class SearchParseService extends DefaultHandler {
	private AudioFiles audioFiles;
	private AudioFile audioFile;
	private StringBuilder sbBuffer = new StringBuilder();

	public AudioFiles getAudioFiles(InputStream xmlStream) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		parser.parse(xmlStream, this);

		return this.audioFiles;
	}

	@Override
	public void startDocument() throws SAXException {
		this.audioFiles = new AudioFiles();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if ("file".equals(qName)) {
			this.audioFile = new AudioFile();
			this.audioFile.setId(UUID.randomUUID().toString());
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		String content = this.sbBuffer.toString().trim();
		if (this.audioFile != null) {
			// file 或者其下面的元素
			if ("description".equals(qName)) {
				this.audioFile.setDescription(content);
			} else if ("downloads-count".equals(qName)) {
				content = content.replaceAll("[, \n]", "");
				long downloadCount = Long.parseLong(content);
				this.audioFile.setDownloadCount(downloadCount);
			} else if ("url".equals(qName)) {
				String url = content.trim();
				Mp3FileInfoAPI mp3Info = new Mp3FileInfoAPI();
				mp3Info.getMp3FileInfo(url);
				int bitrate = mp3Info.getBitrate();
				this.audioFile.setBitrate(bitrate);
				int len = mp3Info.getDuration();
				this.audioFile.setDuration(len);
				
				SimpleAPI api = new SimpleAPI();
				String directUrl = api.getDirectLink(url);
				this.audioFile.setDownloadUrl(directUrl);
			} 
//			else if ("flash-preview-url".equals(qName)) {
//				this.audioFile.setDownloadUrl(content);
//			} 
			else if ("name".equals(qName)) {
				this.audioFile.setFilename(content);
			} else if ("size".equals(qName)) {
				this.audioFile.setSize(content);
			} else if ("file".equals(qName)) {
				this.audioFiles.getFiles().add(this.audioFile);
				this.audioFile = null;
			} 
		} else {
			// search-result 下面的元素
			if ("total-files".equals(qName)) {
				content = content.replaceAll("[, \n]", "");
				int value = Integer.parseInt(content);
				this.audioFiles.setTotalCount(value);
			} else if ("pages-total".equals(qName)) {
				content = content.replaceAll("[, \n]", "");
				int value = Integer.parseInt(content);
				this.audioFiles.setPageCount(value);
			} else if ("files-per-page".equals(qName)) {
				content = content.replaceAll("[, \n]", "");
				int value = Integer.parseInt(content);
				this.audioFiles.setPageSize(value);
			} else if ("start".equals(qName)) {
				content = content.replaceAll("[, \n]", "");
				int value = Integer.parseInt(content);
				this.audioFiles.setStart(value);
			} else if ("query".equals(qName)) {
				this.audioFiles.setQuery(content);
			}
		}
		
		this.sbBuffer.setLength(0);
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String content = new String(ch, start, length);
		this.sbBuffer.append(content);
	}
}